package com.jinxin.platform.cdacscontrolmgr.service.impl;

import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdacscontrolmgr.bean.CdacscontrolmgrRegistInfo;
import com.jinxin.platform.cdacscontrolmgr.common.Constant;
import com.jinxin.platform.cdacscontrolmgr.mapper.CdacscontrolmgrRegistInfoMapper;
import com.jinxin.platform.cdacscontrolmgr.rpc.CdacscontrolmgrRpc;
import com.jinxin.platform.cdacscontrolmgr.service.ICdacscontrolkmgrRegistInfoService;
import com.jinxin.platform.cdacscontrolmgr.task.CdacscontrolmgrQuartzScheduler;
import com.jinxin.platform.cdacscontrolmgr.utils.CronUtil;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-10-21
 * @Author: Lingnan
 **/
@Service
@Slf4j
public class CdacscontrolkmgrRegistInfoServiceImpl implements ICdacscontrolkmgrRegistInfoService {
    @Autowired
    private CdacscontrolmgrRegistInfoMapper registInfoMapper;
    @Autowired
    private CdacscontrolmgrRpc cdacscontrolmgrRpc;
    @Autowired
    private CdacscontrolmgrQuartzScheduler scheduler;

    @Override
    public List<CdacscontrolmgrRegistInfo> list(HashMap param) {
        return registInfoMapper.list(param);
    }

    @Override
    public JsonResult delete(String id) {
        if(StringUtils.isEmpty(id)){
            return JsonResult.error("Id不存在");
        }
        CdacscontrolmgrRegistInfo registInfo = registInfoMapper.selectByKey(id);
        if(registInfo == null){
            return JsonResult.error("人员信息不存在");
        }
        JsonResult jsonResult = cdacscontrolmgrRpc.deletePortraitData(id,registInfo.getProjectId());
        if(jsonResult.getSuccess()){
            int result = registInfoMapper.deleteByKey(id);
            if(result > 0){
                return JsonResult.ok("删除成功","");
            }
            return JsonResult.error("删除失败");
        }
        return JsonResult.error("删除失败");
    }

    @Override
    public JsonResult generateTempPwd(CdacscontrolmgrRegistInfo record) {
        if(record == null){return JsonResult.error("参数错误");}
        String visitDate = record.getVisitDate();
        String comeinTime = record.getComeinTime();
        String leaveTime = record.getLeaveTime();
        if(StringUtils.isEmpty(visitDate) || StringUtils.isEmpty(comeinTime) || StringUtils.isEmpty(leaveTime)){
            return JsonResult.error("到访时间不能为空");
        }
        String[] dateArr = visitDate.split("-");
        String year = Integer.parseInt(dateArr[0])+"";
        String month = Integer.parseInt(dateArr[1])+"";
        String day = Integer.parseInt(dateArr[2])+"";
        String[] timeArr = comeinTime.split(":");
        String hour = Integer.parseInt(timeArr[0])+"";
        String minutes = Integer.parseInt(timeArr[1])+"";
        String cron = CronUtil.getCron(year,"*",month,day,hour,minutes,"0");
        try {
            scheduler.addJob("", Constant.QUZRTZ_GROUP_PREFIX,cron);
        } catch (SchedulerException e) {
            log.error(e.toString());
        }
        return null;
    }

    @Override
    public CdacscontrolmgrRegistInfo getById(String id) {
        return registInfoMapper.selectByKey(id);
    }
}
